﻿using OABackground.Entities;
using System.Collections.Generic;
using System.Linq;

namespace OABackground.Infrastructure
{
    public class MaterialapplyRepository
    {
        public void Add(string departmentid, string teacherid, string goodsname, int goodsnum, float goodsprice, string remarks)
        {
            using (oaContext context = new oaContext())
            {
                Materialapply materialapply = new Materialapply
                {
                    Departmentid = departmentid,
                    Tid = teacherid,
                    Goodsname = goodsname,
                    Goodsnum = goodsnum,
                    Goodsprice = goodsprice,
                    Remarks = remarks,
                    State = 0  //新建物资申请状态默认未处理；0-未处理1-同意2-拒绝
                };
                context.Materialapply.Add(materialapply);
                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            using (oaContext context = new oaContext())
            {
                var materialapply = context.Materialapply.FirstOrDefault(x => x.Id == id);
                context.Materialapply.Remove(materialapply);
                context.SaveChanges();
            }
        }
        public Entities.Materialapply Query(int id)
        {
            using (var context = new oaContext())
            {
                var materialapply = context.Materialapply.FirstOrDefault(x => x.Id == id);
                var teacherName = context.Teacherinfomation.FirstOrDefault(x => x.Teacherid == materialapply.Tid).Tname;
                var departmentname = context.Section.FirstOrDefault(x => x.Id == materialapply.Departmentid).Name;
                materialapply.Departmentid = departmentname;
                materialapply.Tid = teacherName;
                materialapply.T = null;
                materialapply.Department = null;
                return materialapply;
            }
        }
        public List<Materialapply> QueryAllMaterialapply()
        {
            List<Materialapply> materialapplyList = new List<Materialapply>();
            var materialapplyListTemp = new List<Materialapply>(); //处理前
            using (var context = new oaContext())
            {
                materialapplyListTemp = (from u in context.Materialapply
                                         select u
                                ).ToList();

                foreach (var m in materialapplyListTemp)
                {
                    var teacherName = context.Teacherinfomation.FirstOrDefault(x => x.Teacherid == m.Tid).Tname;
                    var departmentname = context.Section.FirstOrDefault(x => x.Id == m.Departmentid).Name;
                    m.Departmentid = departmentname;
                    m.Tid = teacherName;
                    m.T = null;
                    m.Department = null;
                    materialapplyList.Add(m);
                }
            }
            return materialapplyList;
        }
        public List<Materialapply> QueryMaterialapplybyDid(string departmentid)
        {
            List<Materialapply> materialapplyList = new List<Materialapply>();
            using (var context = new oaContext())
            {
                materialapplyList = (from u in context.Materialapply
                                     where u.Departmentid.Equals(departmentid)
                                     select u
                                ).ToList();
                context.SaveChanges();
            }
            return materialapplyList;
        }
        public List<Materialapply> QueryMaterialapplybyState(int state)
        {
            List<Materialapply> materialapplyList = new List<Materialapply>();
            using (var context = new oaContext())
            {
                materialapplyList = (from u in context.Materialapply
                                     where u.State == state
                                     select u
                                ).ToList();
                context.SaveChanges();
            }
            return materialapplyList;
        }
        public List<Materialapply> QueryMaterialapplybyDS(string departmentid, int state)
        {
            List<Materialapply> materialapplyList = new List<Materialapply>();
            using (var context = new oaContext())
            {
                materialapplyList = (from u in context.Materialapply
                                     where u.State == state && u.Departmentid.Equals(departmentid)
                                     select u
                                ).ToList();
                context.SaveChanges();
            }
            return materialapplyList;
        }
        public void Update(Materialapply materialapply)
        {
            using (var context = new oaContext())
            {
                context.Materialapply.Update(materialapply);
                context.SaveChanges();
            }
        }
    }
}
